<?php
declare(strict_types=1);
namespace Service\EsService;
use Service\BaseService;
use Service\Code\Normal;
use App\Amqp\Producer\StoreESMessageProducer;
use App\Amqp\Producer\ChannelESMessageProducer;
use Hyperf\Amqp\Producer;
use Hyperf\Utils\ApplicationContext;

/**
 * ES发布消息中心
 */
class EsPublishMsgService extends BaseService
{
    /**
     * 切割数据大小分页
     * @var int
     */
    protected $slice_size = 100;
    
    /**
     * 店铺商品ES消息服务数据同步
     * @param array $msg
     */
    public function storeESMessageProducer(array $data)
    {
        try {
            $message = new StoreESMessageProducer($data);
            $producer = ApplicationContext::getContainer()->get(Producer::class);
            $result = $producer->produce($message,true);
            if($result) {
                return Normal::returnCode(Normal::SUC,"发布成功");
            }
            return Normal::returnCode(Normal::FAIL,"发布消息队列失败");
        } catch ( \Throwable $th ) {
            return Normal::returnCode(Normal::FAIL,"发布消息队列失败【{$th->getMessage()}】");
        }
    }
    
    /**
     * 渠道商品ES消息服务数据同步
     * @param array $msg
     */
    public function channelESMessageProducer(array $data)
    {
        try {
            $message = new ChannelESMessageProducer($data);
            $producer = ApplicationContext::getContainer()->get(Producer::class);
            $result = $producer->produce($message,true);
            if($result) {
                return Normal::returnCode(Normal::SUC,"发布成功");
            }
            return Normal::returnCode(Normal::FAIL,"发布消息队列失败");
        } catch ( \Throwable $th ) {
            return Normal::returnCode(Normal::FAIL,"发布消息队列失败【{$th->getMessage()}】");
        }
    }

    /**
     * 推送搜索服务数据
     * @param  $data
     * @param string $type
     * @param string $method
     */
    public function pushNew($data,string $type,string $method)
    {
        if($method=='delete'){
            $actionType='CommodityBatchDel';
        }elseif($method=='deleteAll'){
            $actionType='CommodityAllDel';
        }else{
            $actionType='CommodityChange';
        }
                
        $num = 0;
        if(is_array($data)) {
            $num = count($data);
        }
        
        if( $num > $this->slice_size && $actionType <> 'CommodityAllDel' )
        {
            $flag = true;
            $i    = 0;
            while ($flag)
            {
                $new = array_slice($data,$i,$this->slice_size);
                if($new)
                {
                    $i += $this->slice_size;
                    if( $type=='store') 
                    {
                        $res=$this->storeESMessageProducer(['actionType'=>$actionType,'datas'=>$new]);
                    }
                    else
                    {
                        $res=$this->channelESMessageProducer(['actionType'=>$actionType,'datas'=>$new]);
                    }
                }
                else
                {
                    $flag=false;
                }
            }
        }
        else
        {
            if( $type == 'store' )
            {
                $res=$this->storeESMessageProducer(['actionType'=>$actionType,'datas'=>$data]);
            }
            else
            {
                $res=$this->channelESMessageProducer(['actionType'=>$actionType,'datas'=>$data]);
            }
        }
        
        return $res;
    }
}